package com.shioirikukuri.mq;

import com.shioirikukuri.ws.OrderWsListener;
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.websocket.Session;
import java.util.concurrent.TimeUnit;

@Component
@RocketMQMessageListener(
        consumerGroup = "WSListener",
        topic = MQConstants.ORDER_RESULT_TOPIC,
        messageModel = MessageModel.BROADCASTING
)
public class OrderReasultMQListener implements RocketMQListener<OrderMQResult> {
    @Override
    @SneakyThrows
    public void onMessage(OrderMQResult message) {
        System.out.println(JSON.toJSONString(message));
        String token =message.getToken();
        int count = 0;
        do{
            Session session = OrderWsListener.clients.get(token);
            if(session!=null){
                session.getBasicRemote().sendText(JSON.toJSONString(message));
                return;
            }
            TimeUnit.MILLISECONDS.sleep(1000);
            count ++;
        }while (count<10);
        System.out.println("没有找到映射关系结束通知");
    }

}
